﻿using System.Text;
using Nancy;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace FeatureServer.Services
{
    public static class Utilities
    {
        public static string getJSON(dynamic Result)
        {
            if ((Result is GISServer.Core.Client.GeometryService.Root) || (Result is GISServer.Core.Catalog.Catalog))
            {
                return JsonConvert.SerializeObject(Result, Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, ContractResolver = new CamelCasePropertyNamesContractResolver() });
            }
            return null;
        }

        public static Nancy.Response GetJsonResponseObject(string resultstring)
        {
            var jsonBytes = Encoding.UTF8.GetBytes(resultstring);
            return new Response
            {
                ContentType = "text/html",
                StatusCode = HttpStatusCode.OK,
                Contents = s => s.Write(jsonBytes, 0, jsonBytes.Length)
            };
            // application/json
        }

    }
}